package com.party.refund.web.controller.analysis;


import com.party.common.constant.Constant;
import com.party.core.model.crowdfund.Project;
import com.party.core.model.order.RefundPlan;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.order.IRefundPlanService;
import com.party.refund.biz.RefundPlanBizService;
import com.party.refund.biz.SupportBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("")
public class TestController {

    @Autowired
    private IRefundPlanService refundPlanService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private SupportBizService supportBizService;

    @Autowired
    private RefundPlanBizService refundPlanBizService;

    Logger logger = LoggerFactory.getLogger(getClass());

    @ResponseBody
    @RequestMapping(value = "test")
    public String test() {
        logger.info("abc");
       return "123";
    }

    /**
    * @Author hzq
    * @Description //手动测试退款
    * @Date 11:58 2019/5/28
    * @param goalId
    * @return
    **/
    @ResponseBody
    @RequestMapping(value = "testRefund")
    public void testRefund(String goalId) {
        try {
            List<String> list = SupportBizService.list;
            logger.info("list清除前数据："+ list.size());
            list.clear();//清除之前list中的余额不足的商户号
           logger.info("list清除后数据："+ list.size());
            RefundPlan refundPlan = refundPlanService.getByGoalId(goalId);
            if(Constant.PROJECT.equals(refundPlan.getType())){
                //众筹者下面所有的支持者退款
                Project project = projectService.get(refundPlan.getGoalId());
                refundPlanBizService.refund(project);
            }
            if(Constant.SUPPORT.equals(refundPlan.getType())){
                //单个支持者退款
                refundPlanBizService.sendRefund(refundPlan.getGoalId());
            }
            refundPlan.setResult("1");//进队列了，就设置已经执行
            refundPlan.setStatus("0");//设置这条记录为无效
            refundPlan.setUpdateDate(new Date());
            refundPlanService.update(refundPlan);
            logger.info("测试退款结束");
        } catch (Exception e) {
            logger.info("测试退款异常",e);
        }
    }
}
